package game.block.screen;

import game.block.Constants;
import game.block.Locator;
import game.block.game.GameState;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class LevelScreen implements Screen, Constants {
    private BitmapFont white, white96;
    private Stage stage;
    private Table table;
    private ArrayList<TextButton> buttonLevels;
    private TextButton buttonBack;
    private Label heading;
    private Skin skin;
    private TextureAtlas atlas;
    private TextButtonStyle textButtonStyle;
    private final GameState gs;
    private final ArrayList<TransitionEffect> effects;
    private Texture background;
    private SpriteBatch uiBatch;
    public static int COUNT=3;

    public LevelScreen(final GameState gs) {
        uiBatch=new SpriteBatch();
        background = new Texture(BACKGROUNDMAIN);
        background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        stage = new Stage();
        white = new BitmapFont(Gdx.files.internal("data/white2.fnt"), false);
        white96 = new BitmapFont(Gdx.files.internal("data/white2.fnt"), false);
        atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
        skin = new Skin(atlas);
        table = new Table(skin);
        
        this.gs = gs;
        effects = new ArrayList<TransitionEffect>();

        textButtonStyle = new TextButtonStyle();
        textButtonStyle.up = skin.getDrawable("button.up");
        textButtonStyle.down = skin.getDrawable("button.down");
        textButtonStyle.pressedOffsetX = 1;
        textButtonStyle.pressedOffsetY = -1;
        textButtonStyle.font = white;

        final Screen current = this;
        effects.add(new FadeOutTransitionEffect(0.5f));
        effects.add(new FadeInTransitionEffect(0.5f));
        final Screen nextMain = gs.getMainMenu();
        // back button creation
        buttonBack = new TextButton("Back", textButtonStyle);
        buttonBack.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Locator.getAudio().playSound(SOUND.HOVER.getValue());
                Screen transitionScreen = new TransitionScreen(Locator.getGame(), current, nextMain, effects);
                Locator.getGame().setScreen(transitionScreen);
            }
        });
        buttonBack.pad(10.0f);

        buttonLevels = new ArrayList<TextButton>();
        createButtonLevels();
        // create title
        LabelStyle headingStyle = new LabelStyle(white96, Color.WHITE);
        heading = new Label("Level Select", headingStyle);
        createTable();
        stage.addActor(table);
    }
    
    public void createTable(){
    	table.setBounds(0, 0, Gdx.graphics.getWidth()*0.6f, Gdx.graphics.getHeight());
    	table.add(heading).left().pad(Gdx.graphics.getWidth() *0.02f).top().expandY();
        table.row();
        for (int i = 0; i < Locator.getMaxLevel(); i++) {
            if (i % 5 == 0) {
                table.row();
            }
            table.add(buttonLevels.get(i)).uniform().expandX().expandY();
        }
        table.row();
        table.add(buttonBack).expandX().left().pad(Gdx.graphics.getWidth() *0.02f);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        uiBatch.begin();
        uiBatch.draw(background, 0, 0, background.getWidth() * COUNT, background.getHeight() * COUNT, 0, COUNT, COUNT, 0);
        uiBatch.end();
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        stage.setViewport(width, height, true);
        table.invalidateHierarchy();
        table.setSize(width*0.75f, height);
    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);

        effects.clear();
        effects.add(new FadeOutTransitionEffect(0.5f));
        effects.add(new FadeInTransitionEffect(0.5f));
        buttonLevels.clear();
        table.clear();
        createButtonLevels();
        createTable();
    }

    public void createButtonLevels() {
        final Screen next = this;
        // create buttons
        for (int i = 0; i < Locator.getMaxLevel(); i++) {
            final int level = i + 1;
            TextButton buttonLevel = new TextButton(String.valueOf(level), textButtonStyle);
            buttonLevel.addListener(new ClickListener() {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    Locator.setLevel(level );
                    gs.getBoardScreen().setLevel(level);
                    Locator.getAudio().playSound(SOUND.HOVER.getValue());
                    Screen transitionScreen = new TransitionScreen(Locator.getGame(), next, gs.getBoardScreen(), effects);
                    Locator.getGame().setScreen(transitionScreen);
                }
            });
            buttonLevels.add(buttonLevel);
        }
    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        stage.dispose();
        atlas.dispose();
        skin.dispose();
        white.dispose();
    }

}
